﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class UserManager : Form
    {
        public UserManager()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;
        private ListViewGroup collectorListViewGroup;
        private ListViewGroup adminListViewGroup;
        private ListViewGroup viewerListViewGroup;

        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            collectorListViewGroup = new ListViewGroup("Collectors");
            adminListViewGroup = new ListViewGroup("Admins");
            viewerListViewGroup = new ListViewGroup("Viewers");
            lvwUsers.Groups.Add(collectorListViewGroup);
            lvwUsers.Groups.Add(adminListViewGroup);
            lvwUsers.Groups.Add(viewerListViewGroup);
            LoadUsers();
        }

        private void LoadUsers()
        {
            try
            {
                if (eventScavengerDAL != null)
                {
                    lvwUsers.Items.Clear();
                    List<EventScavengerUser> users = eventScavengerDAL.GetUserList();
                    foreach (EventScavengerUser user in users)
                    {
                        ListViewItem lvi = new ListViewItem(user.UserName);
                        if (user.UserType == UserType.Collector)
                            lvi.Group = collectorListViewGroup;
                        else if (user.UserType == UserType.Admin)
                            lvi.Group = adminListViewGroup;
                        else
                            lvi.Group = viewerListViewGroup;
                        lvi.Tag = user;
                        lvwUsers.Items.Add(lvi);
                    }                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvwUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonRemove.Enabled = lvwUsers.SelectedItems.Count > 0;
            removeUserToolStripMenuItem.Enabled = lvwUsers.SelectedItems.Count > 0;
        }

        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            AddUser addUser = new AddUser();
            if (addUser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                EventScavengerUser user = addUser.SelectedUser;
                try
                {
                    eventScavengerDAL.AddUser(user);                    
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains("already exists in the current database"))
                        MessageBox.Show(ex.Message, "Add user", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                LoadUsers();
            }
        }

        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            if (lvwUsers.SelectedItems.Count > 0)
            {
                try
                {
                    if (MessageBox.Show("Are you sure you want to remove this user(s)?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvwUsers.SelectedItems)
                        {
                            EventScavengerUser user = (EventScavengerUser)lvi.Tag;
                            eventScavengerDAL.DeleteUser(user);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Add user", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                LoadUsers();
            }
        }

        private void toolStripButtonReinitialize_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to attempt to re-initialize the Database User Roles?\r\nThis will not remove the roles if they already exist in the database.", "Re-initialize Database Roles", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    eventScavengerDAL.ReInitializeDBRoles();
                    MessageBox.Show("Done", "Re-initialize Database Roles", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Re-initialize Database Roles", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
